/***********************************************************************************************
 * Copyright (C) 2022 China Southern Power Grid Shenzhen Digital Grid Research Institute Co.,Ltd
 * All Rights Reserved.
 * 本软件为南方电网深圳数字电网研究院有限公司开发研制。
 * 未经本公司正式书面同意，其他任何个人、团体不得使用、复制、修改或发布本软件。
 ***********************************************************************************************/

package com.lrj.email;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

/**
 * @version 10月 11, 2023
 * @since JDK1.8
 */
@Configuration
@EnableConfigurationProperties(EmailProperties.class)
public class EmailServiceAutoConfiguration {
    @Autowired
    private EmailProperties emailProperties;

    @Bean
    public EmailService emailService() {
        if (!StringUtils.hasText(emailProperties.getUsername())) {
            throw new RuntimeException("请配置邮箱账号");
        }

        if (!StringUtils.hasText(emailProperties.getAuthorizeCode())) {
            throw new RuntimeException("请配置邮箱授权码");
        }

        EmailService emailService = new EmailService();
        emailService.setUsername(emailProperties.getUsername());
        emailService.setAuthorizeCode(emailProperties.getAuthorizeCode());
        return emailService;
    }
}
